<template>
  <body>
    <div class="img">
      <img :src="logo2" alt="" />
    </div>
    <div class="login">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="账号" prop="phone">
          <el-input v-model="ruleForm.phone" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            v-model="ruleForm.pass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkpass">
          <el-input
            v-model="ruleForm.checkpass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item class="bottom">
          <el-button
            type="success"
            style="margin-left: 50px"
            plain
            @click="submitForm(ruleFormRef)"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- <el-alert title="success alert" type="success" center show-icon /> -->
  </body>
</template>

<script setup>
import logo2 from "../assets/logo2.png";
import { user_register } from "../utils/api";
import { ref } from "vue";
// import { ElMessage } from "element-plus";
// import { useCounterStore } from "../stores/counter";
import axios from "axios";
import { useRouter } from "vue-router";
var $router = useRouter();
const ruleFormRef = ref();
const ruleForm = ref({
  phone: "",
  pass: "",
  checkpass: "",
});
const validatePhone = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入账号"));
  } else if (/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(value) == false) {
    callback(new Error("请输入合法的手机号"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};
const validateCheckpass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    callback();
  }
};

const rules = ref({
  pass: [{ validator: validatePass, trigger: "change" }],
  phone: [{ validator: validatePhone, trigger: "change" }],
  checkpass: [{ validator: validateCheckpass, trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (ruleForm.value.pass != ruleForm.value.checkpass) {
        ElMessage({
          message: "两次输入的密码不一致",
          type: "error",
          center: true,
          grouping: true,
          duration: 1000,
        });
      } else if (
        ruleForm.value.pass.length < 6 &&
        ruleForm.value.checkpass.length < 6
      ) {
        ElMessage({
          message: "密码的长度不够合格",
          type: "error",
          center: true,
          grouping: true,
          duration: 1000,
        });
      } else {
        user_register({
          phone: Number(ruleForm.value.phone),
          pass: ruleForm.value.pass,
        }).then((res) => {
          if (res.data.code == 200) {
            ElMessage({
              message: "注册成功",
              type: "success",
              center: true,
              grouping: true,
              duration: 1000,
            });
            setTimeout(() => {
              $router.push("/login");
            }, 1000);
          } else {
            ElMessage({
              message: res.data.msg,
              type: "error",
              center: true,
              grouping: true,
              duration: 1000,
            });
          }
          console.log(res.data);
          console.log(res.data.list);
        });
      }
    } else {
      console.log("error submit!");
      return false;
    }
  });
};
//  axios
//       .post("/api/user/login", {
//         phone: ruleForm.value.phone,
//         pass: ruleForm.value.pass,
//       })
//       .then((res) => {
//         if (res.data.code == 200) {
//           ElMessage({
//             message: "登录成功",
//             type: "success",
//             center: true,
//             grouping: true,
//             duration: 1500,
//           });
//           $router.push("/");
//         } else {
//           ElMessage({
//             message: "登录失败",
//             type: "error",
//             center: true,
//             grouping: true,
//             duration: 1500,
//           });
//         }
//       });
//     //   console.log(ruleForm.value.phone);
</script>


<style lang="scss" scoped>
html {
  width: 100vw;
  height: 100vh;
}
body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: url(../assets/bgc.png);
  // background: red;
}
.img {
  width: 400px;
  margin: 200px auto 0;
  img {
    width: 100%;
  }
}
.login {
  width: 400px;
  height: 300px;
  display: flex;
  align-items: center;
  position: absolute;
  text-align: center;
  left: 50%;
  top: 50%;
  margin-top: -150px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid #ccc;
  border-radius: 10px;
  margin-left: -200px;
}
.el-button {
  margin-top: 20px;
  margin-left: 20px;
}
</style>